package com.chalk.controller;


import com.chalk.common.util.Result;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.News;
import com.chalk.service.NewsService;
import com.chalk.vo.NewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author glq
 * @since 2020-12-19
 */

@RestController
@RequestMapping("/v1/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    /**
     * 资讯分页
     *
     * @param map
     * @return
     * @throws ColumnNotEmptyException
     */
    @GetMapping("getNewsForPage")
    public Result getNewsForPage(@RequestParam Map<String, Object> map) throws ColumnNotEmptyException {
        return Result.success(newsService.getNewsForPage(map));
    }

    /**
     * 新增资讯
     *
     * @param newsVo
     * @return
     */
    @PostMapping("saveNews")
    public Result saveNews(@RequestBody @Valid NewsVo newsVo) {
        Boolean f = newsService.saveNews(newsVo);
        return f?Result.success("操作成功！"):Result.fail("操作失败");
    }

    /**
     * 修改资讯
     * @param newsVo
     * @return
     */
    @PostMapping("updateNews")
    public Result updateNews(@RequestBody @Valid NewsVo newsVo){
        if(newsVo.getNewsId()==null){
            return Result.fail("资讯id不能为空!");
        }
        Boolean f=newsService.updateNews(newsVo);
        return f?Result.success("操作成功！"):Result.fail("操作失败");
    }

    /**
     * 删除资讯
     * @param newsId
     * @return
     */
    @GetMapping("deleteNews")
    public Result deleteNews(Integer newsId){
        if(newsId==null){
            return Result.fail("资讯id不能为空!");
        }
        Boolean f=newsService.deleteNews(newsId);
        return f?Result.success("操作成功！"):Result.fail("操作失败");
    }

}

